package com.woyou.ui.api;

import java.util.List;

import com.woyou.service.NetWorkCenter;

import android.content.Context;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;

/**
 * 滚动到底部的监听实现
 * @author longtao.li
 *
 */
@SuppressWarnings("rawtypes")
public class DefScrollListener implements OnScrollListener{
	
	private static final String TAG = "DefScrollHolder";
	
	private List mData;
	private Context mContext;
	private int scrollFlag = -1;
	private DefScrollCallBack mCallBack;
	
	public DefScrollListener( List data, Context context,  DefScrollCallBack callBack) {
		this.mData = data;
		this.mContext = context;
		this.mCallBack = callBack;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if( mData == null || mData.size() == 0 )
			return;

		
		//没有网络不执行
		if( !NetWorkCenter.is3G(mContext) && !NetWorkCenter.isWifi(mContext)  )
			return;
		
		if (view.getLastVisiblePosition() >= totalItemCount - 1) {
			
			if( scrollFlag == view.getLastVisiblePosition() ){
				Log.i(TAG, "scrollFlag="+scrollFlag +",view.getLastVisiblePosition()="+view.getLastVisiblePosition() );
				return;
			}
				
			
			scrollFlag = view.getLastVisiblePosition();
			
			if( mCallBack != null ){
				mCallBack.callBack();
			}
			
		}
		
		
		
	}
	
	public void rest(){
		scrollFlag = -1;
	}
	
	public interface DefScrollCallBack{
		void callBack();
	}

}
